/*
 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
 */

/******************************************************************************
 * @file     board_init.c
 * @brief    CSI Source File for board init
 * @version  V1.0
 * @date     31. June 2018
 ******************************************************************************/

#include <stdio.h>
#include <stdint.h>
#include <board.h>
#include "board_config.h"
#include "sdk_config.h"
#include "io_config.h"
#include "reg_uart_type.h"
#include "reg_base_addr.h"
#include "field_manipulate.h"
#include "reg_rcc.h"
#include "lsuart.h"
#include "platform.h"
#include "calc_acc.h"
#include "cpu.h"
#include "spi_flash.h"
static void board_pinmux_config(void)
{
    uart1_io_init(PB00,PB01);
}

static void uart_init()
{
    REG_FIELD_WR(RCC->APB2EN, RCC_UART1, 1);
    REG_FIELD_WR(UART1->LCR,UART_LCR_BRWEN,1);
    UART1->BRR = UART_BAUDRATE_115200;
    REG_FIELD_WR(UART1->LCR,UART_LCR_BRWEN,0);
    UART1->FCR = UART_FCR_TFRST_MASK | UART_FCR_RFRST_MASK | UART_FCR_FIFOEN_MASK;
    UART1->LCR = FIELD_BUILD(UART_LCR_DLS,UART_BYTESIZE8)|FIELD_BUILD(UART_LCR_STOP,UART_STOPBITS1)
                                  |FIELD_BUILD(UART_LCR_PS,UART_NOPARITY)|FIELD_BUILD(UART_LCR_MSB,0)
                                  |FIELD_BUILD(UART_LCR_RXEN,1)|FIELD_BUILD(UART_LCR_BRWEN,0);
}

extern uint64_t (*idiv_acc_fn)(uint32_t,uint32_t,bool);

void board_yoc_init(void)
{
    clk_switch();
    enter_critical();
    idiv_acc_fn = idiv_acc;
    spi_flash_drv_var_init(true,false);
    calc_acc_init();
    board_pinmux_config();
    uart_init();
    extern int boot_flash_init(void);
    boot_flash_init();
}

#if CONFIG_BOOT_LOG==1
int putchar(int ch)
{
    UART1->TBR = ch;
    while (!(UART1->SR & UART_SR_TEMT));
    return 0;
}
#else
int putchar(int ch)
{
    return 0;
}
#endif

void csi_dcache_clean()
{

}

void cache_restart(void);

void csi_icache_invalid()
{
    //cache_restart();
}